package com.jeff.game.tools.physics;

/**
 * A box in 2D space
 */
public class Box implements Rigid
{
  private final int x;
  private final int y;
  private final int width;
  private final int height;

  /**
   * @param x X position of top left corner
   * @param y Y position of top left corner
   * @param width Width of box
   * @param height Height of box
   */
  public Box(int x, int y, int width, int height)
  {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  /**
   * Checks if this box entirely contains another
   * @param other Other box
   * @return True when other is entirely enclosed by this box, false, otherwise
   */
  public boolean isContains(Rigid rigid)
  {
    Box other = rigid.getEnclosingBox();

    int endX = x + width;
    int otherEndX = other.x + other.width;

    int endY = y + height;
    int otherEndY = other.y + other.height;

    return x <= other.x
        && y <= other.y && endX >= otherEndX && endY >= otherEndY;
  }

  /**
   * @return the four quarters of this box #see Region for ordering
   */
  public Box[] quarter()
  {
    int hw = width / 2;
    int hh = height / 2;

    Box[] subboxes = new Box[4];

    subboxes[Region.NORTHWEST.ordinal()] = new Box(x, y, hw, hh);
    subboxes[Region.NORTHEAST.ordinal()] = new Box(x + hw, y, hw, hh);
    subboxes[Region.SOUTHEAST.ordinal()] = new Box(x + hw, y + hh, hw, hh);
    subboxes[Region.SOUTHWEST.ordinal()] = new Box(x, y + hh, hw, hh);

    return subboxes;
  }

  @Override
  public Box getEnclosingBox()
  {
    return this;
  }
}
